﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CoTuong
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public enum GameState {MenuScreen, HighScoreScreen, GameScreen, SubMenu, LoadGame, WaitCommand};
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D background_img;
        

        public static int gameStateCurrent;
        public static bool isEndGame;
        public static Vector2 mouseClickedPosition;
        MouseState mouseStatePrevious;
        public static Vector2 mousePosition;

        Menu menu;
        VanCo vanCo;
        SubMenu subMenu;
        TextScreen txtScreen;
        HighScore highScore;
        NetworkGame networkGame;
        public static SaveData saveData;

        public static bool isPauseGame;

        public Game1()
        {
            
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
            Components.Add(new GamerServicesComponent(this));
       
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            gameStateCurrent = (int)GameState.MenuScreen;
            isEndGame = false;
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            background_img = Content.Load<Texture2D>(@"Images\background");
            
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            
            if (!isPauseGame)
            {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();

                #region Xử lý thao tác chuột
                MouseState mouseStateCurrent = Mouse.GetState();
                mouseClickedPosition.X = -1;
                mouseClickedPosition.Y = -1;

                mousePosition.X = mouseStateCurrent.X;
                mousePosition.Y = mouseStateCurrent.Y;

                if (mouseStateCurrent.LeftButton == ButtonState.Pressed &&
                    mouseStatePrevious.LeftButton == ButtonState.Released)
                {
                    mouseClickedPosition.X = mouseStateCurrent.X;
                    mouseClickedPosition.Y = mouseStateCurrent.Y;
                }
                mouseStatePrevious = mouseStateCurrent;
                #endregion

                switch (gameStateCurrent)
                {
                    case (int)GameState.WaitCommand:
                        if (Menu.CreateNewGame)
                        {
                            gameStateCurrent = (int)GameState.GameScreen;
                            Menu.CreateNewGame = false;
                        }
                        else if (Menu.CreateNetwork)
                        {
                            if (networkGame != null)
                                Components.Remove(networkGame);
                            networkGame = new NetworkGame(this);
                            Components.Add(networkGame);
                            Menu.CreateNetwork = false;
                        }

                        else if (Menu.LoadGame)
                        {
                            gameStateCurrent = (int)GameState.LoadGame;
                            Menu.LoadGame = false;
                        }

                        else if (Menu.ShowHighScore)
                        {
                            gameStateCurrent = (int)GameState.HighScoreScreen;
                            Menu.ShowHighScore = false;
                        }

                        else if (isEndGame == true)
                        {
                            vanCo.Enabled = false;
                            if (Keyboard.GetState().GetPressedKeys().Length > 0)
                            {
                                Components.Remove(vanCo);
                                Components.Remove(txtScreen);
                                vanCo = new VanCo(this);
                                txtScreen = new TextScreen(this);
                                Components.Add(vanCo);
                                Components.Add(txtScreen);
                                isEndGame = false;
                            }
                        }

                        else if (SubMenu.ShowMainMenu)
                        {
                            gameStateCurrent = (int)GameState.MenuScreen;
                            SubMenu.ShowMainMenu = false;
                        }

                        else if (SubMenu.ReturnGame)
                        {
                            vanCo.Enabled = true;
                            Components.Remove(subMenu);
                            SubMenu.ReturnGame = false;
                        }

                        else if (HighScore.BackToMenu)
                        {
                            Components.Remove(highScore);
                            menu.Visible = true;
                            menu.Enabled = true;
                            HighScore.BackToMenu = false;
                        }

                        break;

                    case (int)GameState.GameScreen:
                        if (menu != null)
                            Components.Remove(menu);

                        vanCo = new VanCo(this);
                        txtScreen = new TextScreen(this);
                        Components.Add(vanCo);
                        Components.Add(txtScreen);
                        gameStateCurrent = (int)GameState.WaitCommand;
                        break;

                    case (int)GameState.LoadGame:
                        if (menu != null)
                            Components.Remove(menu);
                        vanCo = new VanCo(this, saveData);
                        txtScreen = new TextScreen(this);
                        Components.Add(vanCo);
                        Components.Add(txtScreen);
                        gameStateCurrent = (int)GameState.WaitCommand;
                        break;

                    case (int)GameState.MenuScreen:
                        if (vanCo != null)
                            Components.Remove(vanCo);
                        if (subMenu != null)
                            Components.Remove(subMenu);
                        if (txtScreen != null)
                            Components.Remove(txtScreen);
                        menu = new Menu(this);
                        Components.Add(menu);
                        gameStateCurrent = (int)GameState.WaitCommand;
                        break;

                    case (int)GameState.SubMenu:
                        vanCo.Enabled = false;
                        subMenu = new SubMenu(this, vanCo);
                        Components.Add(subMenu);
                        gameStateCurrent = (int)GameState.WaitCommand;
                        break;

                    case (int)GameState.HighScoreScreen:
                        menu.Visible = false;
                        menu.Enabled = false;
                        highScore = new HighScore(this);
                        Components.Add(highScore);
                        gameStateCurrent = (int)GameState.WaitCommand;
                        break;
                }

                // TODO: Add your update logic here

                base.Update(gameTime);
            }
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            // TODO: Add your drawing code here
            spriteBatch.Draw(background_img, Vector2.Zero, Color.White);

            spriteBatch.End();
            base.Draw(gameTime);
        }

    }
}
